<?php
function curl_request($url,$method='get',$data=null,$https=true){
    //1.初识化curl
    $ch = curl_init($url);
    //2.根据实际请求需求进行参数封装
    //返回数据不直接输出
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    //如果是https请求
    if($https === true){
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    }
    //如果是post请求
    if($method === 'post'){
        //开启发送post请求选项
        curl_setopt($ch,CURLOPT_POST,true);
        //发送post的数据
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    }
    //3.发送请求
    $result = curl_exec($ch);
    //4.返回返回值，关闭连接
    curl_close($ch);
    return $result;
} 

class wxMenu {

    public function createMenu () {
        $appid = "wx92e23e5b5cfee7c3";
        $appsecret = "1ea79b1c73391dde3b60200a2e7518e8";
        // $result = curl_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx92e23e5b5cfee7c3&secret=1ea79b1c73391dde3b60200a2e7518e8")
        
        $wechat_token_file = __DIR__.'/wxConfig/Wxcatch/wechat_token.txt';  
         $token_data = file_get_contents($wechat_token_file);  
     
        $token_data = json_decode($token_data, true);
        // $ACCESS_TOKEN = $result["access_token"];//通过微信获取access_token接口 获取的token
        $ACCESS_TOKEN = $token_data['token'];//通过微信获取access_token接口 获取的token
        $data = '{
                
         "button":[
            {	
                "type":"view",
                "name":"搜索",
                "url":"http://39.98.134.216:7000/index.html"
             }
              ]
         }';
          //  {
            //       "name":"产品中心",
            //       "sub_button":[
            //         {
            //            "type":"view",
            //            "name":"标准产品",
            //            "url":"http://www.szvdt.com/nav/9.html"
            //         },
            //         {
            //            "type":"view",
            //            "name":"定制产品",
            //            "url":"http://www.szvdt.com/nav/9.html"
            //         },
            //         {
            //            "type":"view",
            //            "name":"经典案例",
            //            "url":"http://www.szvdt.com/nav/12.html"
            //         }]
            //   },
            //   {
            //       "name":"服务",
            //       "sub_button":[
            //         {
            //            "type": "view_limited", 
            //            "name": "公司简介", 
            //            "media_id": ""
            //         },
            //         {
            //            "type": "view_limited", 
            //            "name": "公司动态", 
            //            "media_id": ""
            //         },
            //         {
            //            "type": "view_limited", 
            //            "name": "技术支持", 
            //            "media_id": ""
            //         }]
            //   },
            //   {
            //       "name":"更多",
            //       "sub_button":[
            //         {
            //            "type":"view",
            //            "name":"企业官网",
            //            "url":"http://www.szvdt.com/"
            //         },
            //         {
            //            "type":"view",
            //            "name":"阿里巴巴",
            //            "url":"http://szvdt01.1688.com"
            //         },
            //         {
            //            "type": "view_limited", 
            //            "name": "联系我们", 
            //            "media_id": ""
            //         },
            //         {
            //            "type":"view",
            //            "name":"家校互通",
            //            "url":"http://www.autovdt.com/index.php"
            //         }]
            //   }
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL,
                "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$ACCESS_TOKEN}");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT,
                'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $res= curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Errno' . curl_error($ch);
        }
        curl_close($ch);
        print_r($res);
    }
}

$wx_menu = new wxMenu();
$wx_menu->createMenu();